﻿using System;
using System.IO;

namespace NDBGen
{
    internal static class GeneratorExtensions
    {
        public static IIndentedTextWriter CreateWriter(this FileInfo file, Boolean overwrite = false)
        {
            file.EnsurePath();

            return new IndentedTextWriter(file.Open(overwrite ? FileMode.Create : FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
        }

        public static void EnsurePath(this FileInfo info)
        {
            info.Directory.EnsurePath();            
        }

        public static void EnsurePath(this DirectoryInfo info)
        {
            if (!info.Exists)
            {
                if (info.Parent != null) info.Parent.EnsurePath();
                info.Create();
            }
        }
    }
}